package com.example.dao.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.common.R;
import com.example.common.StatusCode;
import com.example.dao.ApUserDao;
import com.example.pojo.entity.ApUser;
import com.example.pojo.vo.LoginVo;
import com.example.utils.JDBCUtil;
import com.example.utils.JWTUtils;
import org.apache.commons.codec.digest.DigestUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class ApUserDaoImpl implements ApUserDao {

    /**
     * 获取用户信息
     *
     * @param phone
     * @param password
     * @return
     */
    @Override
    public R login(String phone, String password) throws Exception {

        Connection connection = JDBCUtil.connection();
        String sql1 = "select * from ap_user where phone = ?";
        PreparedStatement ps = connection.prepareStatement(sql1);
        ps.setString(1, phone);
        ResultSet resultSet = ps.executeQuery();
        if (!resultSet.next()) {
            return R.error(StatusCode.USER_NOT_EXIST);
        }
        //用户存在，校验密码
        if (! DigestUtils.md5Hex(password).equals(resultSet.getString("password"))) {
            return R.error(StatusCode.PASSWORD_ERROR);
        }
        int userId = resultSet.getInt("id");
        String userName = resultSet.getString("name");
        String phoneNumber = resultSet.getString("phone");
        String image = resultSet.getString("image");
        ApUser apUser = new ApUser();
        apUser.setId(userId);
        apUser.setName(userName);
        apUser.setPhone(phoneNumber);
        apUser.setImage(image);

        JDBCUtil.closeConn(ps, connection);

        //生成token
        String token = JWTUtils.createToken(String.valueOf(apUser.getId()), apUser.getPhone());

        LoginVo loginVo = new LoginVo();
        BeanUtil.copyProperties(apUser, loginVo);
        loginVo.setToken(token);
        JDBCUtil.closeConn(ps,connection);
        return R.ok(loginVo);
    }


    /**
     * 注册
     *
     * @param phone
     * @param password
     * @return
     * @throws Exception
     */
    @Override
    public R register(String phone, String password) throws Exception {
        Connection connection = JDBCUtil.connection();
        String sql1 = "select * from ap_user where phone = ?";
        PreparedStatement ps1 = connection.prepareStatement(sql1);
        ps1.setString(1, phone);
        ResultSet resultSet = ps1.executeQuery();
        if (resultSet.next()) {
            return R.error(StatusCode.PHONE_ALREADY_EXIST);
        }
        //md5加密
        String pwd = DigestUtils.md5Hex(password);
        //注册
        String sql2 = "INSERT INTO ap_user (name,password,phone,status) values (?,?,?,?)";
        PreparedStatement ps2 = connection.prepareStatement(sql2);
        String name = "用户" + phone;
        ps2.setString(1, name);
        ps2.setString(2, pwd);
        ps2.setString(3, phone);
        ps2.setString(4, "0");
        ps2.executeUpdate();

        JDBCUtil.closeConn(ps1,connection);
        JDBCUtil.closeConn(ps2,connection);

        return R.ok("注册成功");

    }
}
